<?php

/**
 * Description of Router
 *
 * @author polacseka
 */
class UAL_Router extends x10_GeneralClass
{
	public $xName = 'Standard felhasználói lekérés kezelő';

	protected $defaultTaskName = 'Default';

	protected $taskSuffix = 'Task';

	protected $taskFolderPath = '';

	protected $registeredTasks = array();

	protected $requestHasTask = FALSE;

	protected $requestHasAction = FALSE;

	public function __construct(x10_Config $config)
	{
		parent::__construct($config);

		$this->taskFolderPath = x10::$RootPath.$this->config->getConfigKey('taskFolderPath');
		$this->registeredTasks = $this->config->getConfigKey('registeredTasks');
	}

	public function getTask()
	{
		$req = x10::getDedicatedObject('Request');
		$taskName = $this->defaultTaskName;
		$this->requestHasTask = FALSE;
		$q = array();
		if (isset($req['http']->get()->arguments['q']))
		{
			$this->requestHasTask = TRUE;
			$qOrig = explode('/', $req['http']->get()->arguments['q']);
			$q = $qOrig;
			$taskName = array_shift($q);
			if (!array_key_exists($taskName, $this->registeredTasks))
			{
				$this->requestHasTask = FALSE;
				$taskName = $this->defaultTaskName;
				$q = $qOrig;
			}
		}

		$taskClass = $this->registeredTasks->$taskName;
		$task = new $taskClass(
			new x10_Config(
				array(
					'q'	=> $q
				)
			)
		);
		
		return $task;
	}

	public function isDefaultTask()
	{
		return !$this->requestHasTask;
	}
}

?>